# 需要导入 request 对象,不是 requests !!!
from unittest import case

from flask import request, Blueprint, redirect, url_for
from classroom_exercises.flask_learn.log_util import logger


user_router = Blueprint('user', __name__, url_prefix='/login')


@user_router.route('', methods=['get'])
def get_info():
    logger.info(f'接收到用户的输入信息是{request.args}')
    # return {'code': 0, 'msg': 'get success'}
    return redirect(url_for('case.cases'))


@user_router.route('1', methods=['post', 'get'])
def form_info():
    request_obj = request.form
    logger.info(f'接收到的用户信息是{request_obj}')
    name = request_obj.get('name')
    password = request_obj.get('passwd')
    logger.info(f'登录信息为:用户名{name},密码{password}')
    return {'code': 0, 'msg': '登录成功'}


@user_router.route('2', methods=['post'])
def json_info():
    request_obj = request.json
    logger.info(f'接收到的用户信息是{request_obj}')
    name = request_obj.get('name')
    password = request_obj.get('passwd')
    password_confirm = request_obj.get('passwd_confirm')
    email = request_obj.get('email')
    logger.info(f'登录信息为:用户名{name},密码{password},确认密码{password_confirm},邮箱是{email}')
    return {'code': 0, 'msg': '用户注册成功'}


@user_router.route('3', methods=['post', 'get'])
def save_file():
    # GET一定要大写
    if request.method == 'GET':
        return {'code': 0, 'msg': '文件查看成功'}
    file = request.files
    logger.info(f'file_obj:{file}')
    file_obj = file.get('file')
    # 将文件对象（file_obj）的文件名（filename）赋给变量file_name
    file_name = file_obj.filename
    logger.info(f'文件名是{file_name}')
    # 保存文件名为file_name的文件
    file_obj.save(file_name)
    return {'code': 0, 'msg': '查看文件成功!!!'}


